package org.devocean.groupbuying.service.impl;

import java.util.List;

import org.devocean.groupbuying.dao.MenuDAO;
import org.devocean.groupbuying.domain.Menu;
import org.devocean.groupbuying.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuDAO menuDAO;

	@Transactional(readOnly = true)
	public Menu findMenuById(Integer id) {
		return menuDAO.findMenuById(id);
	}

	@Transactional
	@CacheEvict(value = "menus", allEntries = true)
	public void deleteMenu(Integer id) {
		menuDAO.deleteMenu(id);
	}

	@Transactional(readOnly = false)
	@Cacheable(value = "menus")
	public List<Menu> findAllMenus() {
		try {
			return menuDAO.findAllMenus();
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Transactional(readOnly = false)
	@Cacheable(value = "menus")
	public List<Menu> getAllMenus(String lang) {
		try {
			return menuDAO.findAllMenus(lang);
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Transactional
	@CacheEvict(value = "menus", allEntries = true)
	public Menu addMenu(Menu menu) {
		return menuDAO.saveMenu(menu);
	}

}
